//
//  NWUserAccount.swift
//  Weibo
//
//  Created by Northwan on 16/1/22.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWUserAccount: NSObject,NSCoding {
    var access_token:String?
    
    var expires_in:NSTimeInterval = 0 {
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            printLog(expiresDate)
        }
    }
    
    //计算并记录accessToken的过期时间
    var expiresDate:NSDate?

    var uid:String?
    
    var screen_name: String?
    var avatar_large: String?
    
    
    init(dict:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description:String{
        let keys = ["access_token","expires_in","uid","screen_name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
       access_token =  aDecoder.decodeObjectForKey("access_token") as? String
       expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
       uid = aDecoder.decodeObjectForKey("uid") as? String
       screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
       avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
